package team.knowers.tourist.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.cron.timingwheel.SystemTimer;
import cn.hutool.cron.timingwheel.TimerTask;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.tourist.common.convention.exception.ClientException;
import team.knowers.tourist.common.database.BaseDO;
import team.knowers.tourist.dao.entity.BookingDO;
import team.knowers.tourist.dao.entity.PaymentDO;
import team.knowers.tourist.dao.mapper.BookingMapper;
import team.knowers.tourist.dao.mapper.PaymentMapper;
import team.knowers.tourist.dto.req.PaymentCreateReqDTO;
import team.knowers.tourist.service.NoticeService;
import team.knowers.tourist.service.PaymentService;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentDO> implements PaymentService {
    private final NoticeService noticeService;
    private final PaymentMapper paymentMapper;
    private final BookingMapper bookingMapper;

    @Override
    public void createPayment(PaymentCreateReqDTO req) {
        LambdaQueryWrapper<PaymentDO> wrapper = Wrappers.lambdaQuery(PaymentDO.class)
                .eq(PaymentDO::getFlightId, req.getFlightId())
                .eq(PaymentDO::getIdentifyNumber, req.getIdentifyNumber())
                .eq(PaymentDO::getIsPaid, 1);
        PaymentDO paymentDO = baseMapper.selectOne(wrapper);
        if (paymentDO != null) {
            throw new ClientException("订单已存在");
        }
        LambdaQueryWrapper<PaymentDO> wrapper1 = Wrappers.lambdaQuery(PaymentDO.class)
                .eq(PaymentDO::getFlightId, req.getFlightId())
                .eq(PaymentDO::getIdentifyNumber, req.getIdentifyNumber())
                .eq(PaymentDO::getIsPaid, 2);
        PaymentDO paymentDO1 = baseMapper.selectOne(wrapper1);
        if (paymentDO1 != null) {
            throw new ClientException("订单已成功支付，请勿重复创建");
        }
        PaymentDO paymentDO2=PaymentDO.builder()
                .isPaid(1)
                .flightId(req.getFlightId())
                .identifyNumber(req.getIdentifyNumber())
                .amount(req.getAmount())
                .touristId(req.getTouristId())
                .flightNo(req.getFlightNo())
                .bookingId(req.getBookingId())
                .build();
        baseMapper.insert(paymentDO2);
    }
    @Override
    public void payPayment(int id) {
        LambdaQueryWrapper<PaymentDO> wrapper = Wrappers.lambdaQuery(PaymentDO.class)
                .eq(PaymentDO::getId, id)
                .eq(PaymentDO::getIsPaid, 1)
                .eq(PaymentDO::getDelFlag, 0);
        Date date=new Date();
        PaymentDO paymentDO = PaymentDO.builder()
                .isPaid(2)
                .paymentHash(generateRandomNumber(1, 1000000000))
                .paymentDate(date)
                .build();
        baseMapper.update(paymentDO, wrapper);
        noticeService.addNoticeWhenPaying(id);
    }

    /**
     * 取消订单
     *
     * @param id
     */
    @Override
    public void cancelPayment(int id) {
        LambdaQueryWrapper<PaymentDO> wrapper = Wrappers.lambdaQuery(PaymentDO.class)
                .eq(PaymentDO::getId, id)
                .eq(PaymentDO::getIsPaid, 1)
                .eq(PaymentDO::getDelFlag, 0);
        PaymentDO paymentDO = PaymentDO.builder()
                .isPaid(3)
                .build();
        baseMapper.update(paymentDO, wrapper);
    }

    @Override
    public List<PaymentDO> viewOrder(int touristId) {
        LambdaQueryWrapper<PaymentDO> wrapper = Wrappers.lambdaQuery(PaymentDO.class)
                .eq(PaymentDO::getTouristId,touristId)
                .eq(PaymentDO::getDelFlag,0)
                .orderByDesc(PaymentDO::getCreateTime);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Boolean getTicket(int touristId, int bookingHash, int paymentHash) {
        PaymentDO paymentDO = baseMapper.selectOne(Wrappers.lambdaQuery(PaymentDO.class)
                .eq(BaseDO::getDelFlag, 0)
                .eq(PaymentDO::getTouristId, touristId)
                .eq(PaymentDO::getPaymentHash, paymentHash));
        if (paymentDO == null) {
            return false;
        }
        BookingDO bookingDO = bookingMapper.selectOne(Wrappers.lambdaQuery(BookingDO.class).eq(BaseDO::getDelFlag, 0)
                .eq(BookingDO::getBookingHash, bookingHash)
                .eq(BookingDO::getId, paymentDO.getId())
                .eq(BookingDO::getTouristId, touristId));
        if (bookingDO == null) {
            return false;
        }
        return true;
    }

    /**
     * 生成随机数
     */
    public static int generateRandomNumber(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

}
